<template>
    <div class="app-container">
        <header-title/>
        <el-form
            :model="queryParams"
            ref="queryForm"
            :inline="true"
            v-show="showSearch"
            label-width="110px"
        >
            <el-form-item label="关键词" prop="keyword">
                <el-input
                    v-model="queryParams.keyword"
                    placeholder="请输入订单号/手机号/订单标题"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="用户" prop="userId">
                <el-select v-model="queryParams.userId" filterable clearable>
                    <el-option
                        v-for="dict in userList"
                        :key="dict.clientUserId"
                        :label="dict.userRealName"
                        :value="dict.clientUserId"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="渠道来源" prop="userChannel">
                <el-select v-model="queryParams.userChannel" filterable clearable>
                    <el-option
                        v-for="dict in dict.type.user_channel"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            
            <el-form-item label="充值状态" prop="rechargeStatus">
                <el-select v-model="queryParams.rechargeStatus" placeholder="请选择充值状态" clearable>
                    <el-option
                        v-for="dict in dict.type.recharge_status"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    >

                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="充值类型" prop="rechargeType">
                <el-select v-model="queryParams.rechargeType" placeholder="请选择充值类型" clearable>
                    <el-option
                        v-for="dict in dict.type.recharge_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                        v-if="dict.value == 3 || dict.value == 4"

                    >
                    </el-option>
                </el-select>
            </el-form-item>
            
            
            <el-form-item label="创建时间" prop="jiTime">
                <el-date-picker
                    v-model="dateRange"
                    type="daterange"
                    value-format="yyyy-MM-dd"
                    @keyup.enter.native="handleQuery"
                    placeholder="选择时间">
                </el-date-picker>
            </el-form-item>

            <el-form-item class="mar-r-20">
                <el-button type="primary" icon="el-icon-search" @click="handleQuery"
                >搜索</el-button
                >
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="onHandlerecharge"
                >手动充值</el-button
                >
            </el-form-item>
        </el-form>
        <el-table
            v-loading="loading"
            :data="tableList"
        >   
            <el-table-column label="充值编号" min-width="130">
                <template slot-scope="scope">
                    {{ scope.row.orderNo ||'-' }}
                </template>
            </el-table-column>
            <el-table-column label="用户渠道 " >
                <template slot-scope="scope">
                    {{selectDictLabel(dict.type.user_channel,scope.row.userChannel) || '-'}}
                </template>
            </el-table-column>
            <el-table-column label="用户信息" min-width="160">
                <template slot-scope="scope">
                    <el-form size="mini" label-suffix="：" class="form-detail-box">
                        <el-form-item label="">
                        <div class="fw-b">{{ scope.row.orderUserName || "-" }}</div>
                            
                        </el-form-item>
                        <el-form-item label="联系方式" class="label-fw-initial">
                            <span>{{ scope.row.phone || "-" }}</span>
                        </el-form-item>
                    </el-form>
                </template>
            </el-table-column>
            <el-table-column label="充值类型" >
                <template slot-scope="scope">
                    {{selectDictLabel(dict.type.recharge_type,scope.row.rechargeType ) ||'-'}}
                </template>
            </el-table-column>
            
            <el-table-column label="当前算力" >
                <template slot-scope="scope">
                    {{ scope.row.currentPoint ||'-' }}
                </template>
            </el-table-column>
            <el-table-column label="本次充值金额" >
                <template slot-scope="scope">
                    {{ scope.row.rechargeAmount ||'-' }}
                </template>
            </el-table-column>
            <el-table-column label="本次充值算力" >
                <template slot-scope="scope">
                    {{ scope.row.rechargePoint ||'-' }}
                </template>
            </el-table-column>
            <el-table-column label="充值状态" >
                <template slot-scope="scope">
                    <el-tag
                        v-if="scope.row.rechargeStatus"
                        :type="
                        [2].includes(scope.row.rechargeStatus)
                            ? 'success'
                           
                            : [1].includes(scope.row.rechargeStatus)
                            ? 'danger'
                            : [0].includes(scope.row.rechargeStatus)
                            ? 'warning':''
                        "
                        >
                        {{
                        selectDictLabel(
                            dict.type.recharge_status,
                            scope.row.rechargeStatus
                        ) || "未知状态"
                        }}
                        </el-tag
                    >
                    
                </template>
            </el-table-column>
            <el-table-column label="充值方式" >
                <template slot-scope="scope">
                    {{
                        selectDictLabel(
                            dict.type.recharge_type,
                            scope.row.rechargeType
                        ) || "未知状态"
                        }}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="100">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-search"
                        @click="handleUpdateDetail(scope.row)"
                    >详情</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        >
        </pagination>
        <el-drawer
            title="订单详情"
            :visible.sync="showDetail"
            direction="rtl"
            :before-close="handleClose"
            size="900px"
        >
            <orderDetail :id="detailId" v-if="showDetail"></orderDetail>
        </el-drawer>
        <!-- 手动充值 -->
        <el-dialog title="充值" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item  label="充值用户" prop="clientUserId">
                            <el-select v-model="form.clientUserId" filterable>
                                <el-option
                                    v-for="dict in userList"
                                    :key="dict.clientUserId"
                                    :label="dict.userRealName + '（'+dict.phone +'）'"
                                    :value="dict.clientUserId"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item  label="充值金额" prop="amount">
                            <el-input v-model="form.amount" placeholder="请输入充值金额">
                                <template slot="append">元</template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item  label="充值算力" prop="rechargePoint">
                            <el-input v-model="form.rechargePoint" placeholder="请输入充值算力"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="open = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import {listOrder,rechargeManual} from '@/api/computeOrderManage/orderList'
    import {listClientUser} from '@/api/operateManage/userList'
    import orderDetail from '../components/orderDetail/detail.vue'

    export default{
        name:'RechargeList',
        components:{orderDetail},
        dicts:['order_status','order_type','point_change_type','recharge_status','recharge_type','ai_type','user_channel'],
        data() {
            return {
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    orderType:1,
                    orderStatus:'',
                    createTimeEnd: '',
                    createTimeStart:'',
                    endTimeEnd:'',
                    endTimeStart:'',
                    keyword:'',
                    pointChangeType: '',
                    rechargeStatus: '',
                    rechargeType: '',
                    useAiType:'',
                    userId: '',
                    userChannel: '',
                   
                },
                showSearch: true,
                total: 0,
                dateRange: [],
                dateRangeTwo:[],
                // 表格数据
                tableList: [],
                loading: false,
                userList: [],
                showDetail: false,
                detailId:'',
                open: false,
                form: {},
                rules: {
                    clientUserId:[
                        { required: true, message: "请先选择用户", trigger: "change" }
                    ],
                    amount:[
                        { required: true, message: "充值金额不能为空", trigger: "change" }
                    ],
                    rechargePoint:[
                        { required: true, message: "充值算力不能为空", trigger: "change" }
                    ],
                }
            }
        },
        created() {
            this.getList()
            this.getUserList()
        },
        methods: {
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        rechargeManual(this.form).then(res=> {
                            this.$modal.msgSuccess(res.msg)
                            this.open = false
                            this.getList()
                        })
                    }
                })
            },
            onHandlerecharge() {
                this.form = {
                    amount:'',
                    clientUserId:'',
                    rechargePoint:''
                }
                this.open = true
            },
            handleClose() {
                this.showDetail = false
            },
            getUserList() {
                listClientUser({pageSize: 10000,pageNum:1}).then(res=> {
                    this.userList = res.data.records
                })
            },
            handleUpdateDetail(row) {
                this.detailId = row.orderNo
                this.showDetail = true
            },
             /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.dateRange = [];
                this.resetForm("queryForm");
                this.handleQuery();
            },
            getList() {
                this.loading = true;
                if (this.dateRange && this.dateRange.length) {
                    this.queryParams.createTimeStart = this.dateRange[0];
                    this.queryParams.createTimeEnd = this.dateRange[1];
                } else {
                    this.queryParams.createTimeStart = undefined;
                    this.queryParams.createTimeEnd = undefined;
                }
                if (this.dateRangeTwo && this.dateRangeTwo.length) {
                    this.queryParams.endTimeStart = this.dateRangeTwo[0];
                    this.queryParams.endTimeEnd = this.dateRangeTwo[1];
                } else {
                    this.queryParams.endTimeStart = undefined;
                    this.queryParams.endTimeEnd = undefined;
                }
                
                listOrder(this.queryParams).then((response) => {
                    this.tableList = response.data.records;
                    this.total = response.data.total;
                    this.loading = false;
                });
            }
        }
    }
</script>